package com.ruoyi.datamanagement.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.datamanagement.mapper.SysSearchTreeMapper;
import com.ruoyi.datamanagement.domain.SysSearchTree;
import com.ruoyi.datamanagement.service.ISysSearchTreeService;

/**
 * 查询树Service业务层处理
 *
 * @author anan
 * @date 2021-03-31
 */
@Service
public class SysSearchTreeServiceImpl extends ServiceImpl<SysSearchTreeMapper,SysSearchTree> implements ISysSearchTreeService
{
    @Autowired
    private SysSearchTreeMapper sysSearchTreeMapper;

    /**
     * 查询查询树
     *
     * @param searchTreeId 查询树ID
     * @return 查询树
     */
    @Override
    public SysSearchTree selectSysSearchTreeById(Long searchTreeId)
    {
        return sysSearchTreeMapper.selectSysSearchTreeById(searchTreeId);
    }

    /**
     * 查询查询树列表
     *
     * @param sysSearchTree 查询树
     * @return 查询树
     */
    @Override
    public List<SysSearchTree> selectSysSearchTreeList(SysSearchTree sysSearchTree)
    {
        return sysSearchTreeMapper.selectSysSearchTreeList(sysSearchTree);
    }

    /**
     * 新增查询树
     *
     * @param sysSearchTree 查询树
     * @return 结果
     */
    @Override
    public int insertSysSearchTree(SysSearchTree sysSearchTree)
    {
        SysSearchTree info = sysSearchTreeMapper.selectSysSearchTreeById(sysSearchTree.getParentId());
        sysSearchTree.setAncestors(info.getAncestors()+","+sysSearchTree.getParentId());
        return sysSearchTreeMapper.insertSysSearchTree(sysSearchTree);
    }

    /**
     * 修改查询树
     *
     * @param sysSearchTree 查询树
     * @return 结果
     */
    @Override
    public int updateSysSearchTree(SysSearchTree sysSearchTree)
    {
        return sysSearchTreeMapper.updateSysSearchTree(sysSearchTree);
    }

    /**
     * 批量删除查询树
     *
     * @param searchTreeIds 需要删除的查询树ID
     * @return 结果
     */
    @Override
    public int deleteSysSearchTreeByIds(Long[] searchTreeIds)
    {
        return sysSearchTreeMapper.deleteSysSearchTreeByIds(searchTreeIds);
    }

    /**
     * 删除查询树信息
     *
     * @param searchTreeId 查询树ID
     * @return 结果
     */
    @Override
    public int deleteSysSearchTreeById(Long searchTreeId)
    {
        return sysSearchTreeMapper.deleteSysSearchTreeById(searchTreeId);
    }

    @Override
    public List<TreeSelect> buildSearchTreeSelect(List<SysSearchTree> searchTrees) {
        List<SysSearchTree> searchTrees1 = buildDeptTree(searchTrees);
        return searchTrees1.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param searchTrees 查询树列表
     * @return 树结构列表
     */
    public List<SysSearchTree> buildDeptTree(List<SysSearchTree> searchTrees)
    {
        List<SysSearchTree> returnList = new ArrayList<SysSearchTree>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysSearchTree searchTree1 : searchTrees)
        {
            tempList.add(searchTree1.getSearchTreeId());
        }
        for (Iterator<SysSearchTree> iterator = searchTrees.iterator(); iterator.hasNext();)
        {
            SysSearchTree searchTree1 = (SysSearchTree) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(searchTree1.getParentId()))
            {
                recursionFn(searchTrees, searchTree1);
                returnList.add(searchTree1);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = searchTrees;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysSearchTree> list, SysSearchTree t)
    {
        // 得到子节点列表
        List<SysSearchTree> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysSearchTree tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysSearchTree> getChildList(List<SysSearchTree> list, SysSearchTree t)
    {
        List<SysSearchTree> tlist = new ArrayList<SysSearchTree>();
        Iterator<SysSearchTree> it = list.iterator();
        while (it.hasNext())
        {
            SysSearchTree n = (SysSearchTree) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getSearchTreeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysSearchTree> list, SysSearchTree t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
